package com.cd2cd.service.impl;

import com.cd2cd.comm.ServiceCode;
import com.cd2cd.domain.SysDict;
import com.cd2cd.domain.SysDictItem;
import com.cd2cd.domain.gen.SysDictCriteria;
import com.cd2cd.domain.gen.SysDictItemCriteria;
import com.cd2cd.mapper.SysDictItemMapper;
import com.cd2cd.mapper.SysDictMapper;
import com.cd2cd.service.SysDictItemService;
import com.cd2cd.service.SysDictService;
import com.cd2cd.util.BeanUtil;
import com.cd2cd.util.StringUtil;
import com.cd2cd.vo.BaseRes;
import com.cd2cd.vo.DataPageWrapper;
import com.cd2cd.vo.SysDictItemVo;
import com.cd2cd.vo.SysDictVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class SysDictItemServiceImpl implements SysDictItemService {


    @Resource
    private SysDictItemMapper sysDictItemMapper;

    @Override
    public BaseRes<DataPageWrapper<SysDictItemVo>> list(Integer currPage, Integer pageSize, SysDictItemVo sysDictItemVo) {

        BaseRes<DataPageWrapper<SysDictItemVo>> res = new BaseRes<>();

        res.setData(new DataPageWrapper<SysDictItemVo>());

        Integer dictId = sysDictItemVo.getDictId();

        if(null == dictId) {
            return res;
        }


        int mysqlLength = pageSize;
        int mysqlOffset = (currPage - 1) * mysqlLength;

        SysDictItemCriteria sysDictItemCriteria = new SysDictItemCriteria();

        sysDictItemCriteria.setMysqlLength(mysqlLength);
        sysDictItemCriteria.setMysqlOffset(mysqlOffset);

        SysDictItemCriteria.Criteria criteria = sysDictItemCriteria.createCriteria();

        criteria.andDictIdEqualTo(dictId);

        String label = sysDictItemVo.getLabel();
        if (StringUtil.isNotBlank(label)) {
            criteria.andLabelLike("%" + label + "%");
        }

        String status = sysDictItemVo.getStatus();
        if (StringUtil.isNotBlank(status)) {
            criteria.andStatusEqualTo(status);
        }

        long count = sysDictItemMapper.countByExample(sysDictItemCriteria);

        res.getData().setTotalCount(count);

        if (count > 0) {
            List<SysDictItem> sysDicts = sysDictItemMapper.selectByExample(sysDictItemCriteria);
            List<SysDictItemVo> rows = BeanUtil.voConvertList(sysDicts, SysDictItemVo.class);
            res.getData().setRows(rows);
        }

        return res;
    }

    @Override
    public BaseRes<SysDictItemVo> detail(Integer dictId,Integer id) {

        BaseRes<SysDictItemVo> res = new BaseRes<>(ServiceCode.SUCCESS);

        if (null == dictId) {
            return res;
        }

        SysDictItem sysDictItem = sysDictItemMapper.selectByPrimaryKey(id);

        if (sysDictItem != null) {
            SysDictItemVo sysDictItemVo = BeanUtil.voConvert(sysDictItem, SysDictItemVo.class);
            res.setData(sysDictItemVo);
        }

        return res;
    }

    @Override
    public BaseRes<String> del(Integer dictId,Integer id) {

        if (null == dictId) {
            return new BaseRes<>(ServiceCode.FAILED);
        }

        sysDictItemMapper.deleteByPrimaryKey(id);

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> add(SysDictItemVo sysDictItemVo) {

        Integer dictId = sysDictItemVo.getDictId();

        if(null == dictId) {
            return new BaseRes<>(ServiceCode.FAILED);
        }


        sysDictItemVo.setCreateTime(new Date());
        sysDictItemVo.setUpdateTime(new Date());
        sysDictItemMapper.insertSelective(sysDictItemVo);

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> modify(SysDictItemVo sysDictItemVo) {

        Integer dictId = sysDictItemVo.getDictId();

        if(null == dictId) {
            return new BaseRes<>(ServiceCode.FAILED);
        }

        sysDictItemVo.setUpdateTime(new Date());
        sysDictItemMapper.updateByPrimaryKeySelective(sysDictItemVo);

        return new BaseRes<>(ServiceCode.SUCCESS);
    }
}
